<?php
namespace com\rainiswalking\sonar_report\lib;
class HttpUtil {
    const HTTP_POST = 'POST';
    const HTTP_GET = 'GET';

    public static function post($url, $params, $opts=array()) {
        $opts['method'] = self::HTTP_POST;
        return self::http_request($url, $params, $opts);
    }

    public static function get($url, $params, $opts=array()) {
        $opts['method'] = self::HTTP_GET;
        return self::http_request($url, $params, $opts);
    }

    static function http_request($url, $params, $opts) {
        $params_str = self::build_params($params);

        $dest_url = $url;
        if ($opts['method'] == self::HTTP_GET) {
            if (strpos($url, '?') !== false) {
                $url .= '?';
            }
            $dest_url .= $params_str;
        }

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $dest_url );
        curl_setopt ( $ch, CURLOPT_FAILONERROR, false );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 60);


        if (isset($opts['header'])) {
            curl_setopt ( $ch, CURLOPT_HTTPHEADER, $opts['header']);
        }
        if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
            curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
        }


        if (isset($opts['method']) && $opts['method'] == self::HTTP_POST) {
            curl_setopt ( $ch, CURLOPT_POST, true );
            curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params_str );
        }

        $response = curl_exec ( $ch );
        if (curl_errno ( $ch )) {
            $curl_error = curl_error ( $ch );
            self::request_log($url, $params,$curl_error,1);
            throw new \Exception ( $curl_error, 0 );
        } else {
            $httpStatusCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
            if (200 !== $httpStatusCode) {
                self::request_log($url, $params,(string)$response . ' httpStatusCode ' .(string)$httpStatusCode,1);
                throw new \Exception ( $response, $httpStatusCode );
            }
        }
        curl_close ( $ch );

        self::request_log($url, $params, $response, 0);

        return $response;//return json_decode($response, true);
    }

    private static function request_log($url, $params, $response, $is_error) {
        $tag = $is_error == 0 ? 'SUCCESS' : 'ERROR';
        $params = is_string($params) ? $params : json_encode($params);
        $msg = date('[Y-m-d H:i:s]')."\t".$tag."\turl:".$url.', params:'.$params."\t".', response:'.$response;
        error_log($msg."\r\n", 3, 'http-request.log');
    }

    /**
     * @param $params
     * @return array|string
     */
    public static function build_params($params)
    {
        /**
         * 如果参数为数组则
         */
        if (is_array($params)) {
            $postBodyString = "";
            foreach ($params as $k => $v) {
                $postBodyString .= "$k=" . urlencode($v) . "&";
            }
            unset ($k, $v);
            if (!empty($postBodyString)) {
                $postBodyString = substr($postBodyString, 0, -1);
                return $postBodyString;
            }
            return $postBodyString;
        } else {
            $postBodyString = $params;
            return $postBodyString;
        }
    }
}